package org.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.domain.entity.BackgroundResource;
import org.chen.mapper.BackgroundResourceMapper;
import org.chen.service.BackgroundResourceService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class BackgroundResourceServiceImpl extends ServiceImpl<BackgroundResourceMapper, BackgroundResource> implements BackgroundResourceService {

    @Override
    public IPage<BackgroundResource> pageList(Page<BackgroundResource> page, String fileType, String keyword) {
        LambdaQueryWrapper<BackgroundResource> queryWrapper = new LambdaQueryWrapper<>();
        
        // 按文件类型筛选
        if (StringUtils.hasText(fileType)) {
            queryWrapper.eq(BackgroundResource::getFileType, fileType);
        }
        
        // 按关键词搜索
        if (StringUtils.hasText(keyword)) {
            queryWrapper.like(BackgroundResource::getName, keyword);
        }
        
        // 按创建时间倒序
        queryWrapper.orderByDesc(BackgroundResource::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    @Override
    public boolean saveBackground(BackgroundResource backgroundResource) {
        return this.save(backgroundResource);
    }

    @Override
    public boolean removeBackground(String id) {
        return this.removeById(id);
    }

    @Override
    public BackgroundResource getBackground(String id) {
        return this.getById(id);
    }
}